-- 参考VSCode Dark+ 主题颜色配置
local cjColors = {
  -- 基础颜色
  foreground = "#D4D4D4",
  background = "#1E1E1E",
  
  -- 语法高亮颜色
  keyword = "#C586C0",
  function_color = "#DCDCAA",
  variable = "#9CDCFE",
  string = "#CE9178",
  number = "#B5CEA8",
  comment = "#6A9955",
  operator = "#D4D4D4",
  type = "#4EC9B0",
  constant = "#569CD6",
  parameter = "#9CDCFE",
  property = "#9CDCFE",
  tag = "#E8BF6A",
  attribute = "#9CDCFE",
  namespace = "#4EC9B0",
  
  -- 特殊颜色
  builtin = "#4EC9B0",
  escape = "#D7BA7D",
  regex = "#D16969",
  markup = "#CE9178",
  error = "#F44747",
  warning = "#FF8800",
  info = "#75BEFF",
  hint = "#B267E6",
}

local highlights = {
    -- 基础组
    cjNormal = { fg = cjColors.foreground, bg = cjColors.background },
    cjComment = { fg = cjColors.comment, italic = true },

    -- 关键字相关
    cjKeyword = { fg = cjColors.keyword },
    cjConditional = { fg = cjColors.keyword },
    cjRepeat = { fg = cjColors.keyword },
    cjLabel = { fg = cjColors.keyword },
    cjOperator = { fg = cjColors.operator },
    cjException = { fg = cjColors.keyword },

    -- 函数相关
    cjFunction = { fg = cjColors.function_color },
    cjFunctionCall = { fg = cjColors.function_color },
    cjMethod = { fg = cjColors.function_color },
    cjConstructor = { fg = "#4EC9B0", bold = true },

    -- 变量相关
    cjVariable = { fg = cjColors.variable },
    cjParameter = { fg = cjColors.parameter },
    cjProperty = { fg = cjColors.property },
    cjField = { fg = cjColors.property },

    -- 类型相关
    cjType = { fg = cjColors.type },
    cjTypeBuiltin = { fg = cjColors.builtin },
    cjInterface = { fg = "#B5CEA8" },
    cjEnum = { fg = "#B5CEA8" },
    cjEnumMember = { fg = "#B5CEA8" },

    -- 常量相关
    cjConstant = { fg = cjColors.constant },
    cjConstantBuiltin = { fg = "#569CD6", bold = true },
    cjNumber = { fg = cjColors.number },
    cjBoolean = { fg = "#569CD6" },

    -- 字符串相关
    cjString = { fg = cjColors.string },
    cjStringEscape = { fg = cjColors.escape, bold = true },
    cjStringRegex = { fg = cjColors.regex },
    cjStringSpecial = { fg = "#CE9178", bold = true },

    -- 标点符号
    cjPunctuation = { fg = cjColors.foreground },
    cjBracket = { fg = cjColors.foreground },
    cjDelimiter = { fg = cjColors.foreground },

    -- 特殊语法
    cjTag = { fg = cjColors.tag },
    cjAttribute = { fg = cjColors.attribute },
    cjNamespace = { fg = cjColors.namespace },

    -- 错误和警告
    cjError = { fg = cjColors.error },
    cjWarning = { fg = cjColors.warning },
    cjInfo = { fg = cjColors.info },
    cjHint = { fg = cjColors.hint },

    -- 文本格式
    cjStrong = { bold = true },
    cjEmphasis = { italic = true },
    cjUnderline = { underline = true },
    cjStrike = { strikethrough = true },
}
-- 应用颜色组
for group, settings in pairs(highlights) do
    vim.api.nvim_set_hl(0, '@'.. group:sub(3):lower() , settings)
end

local M = {
    "nvim-treesitter/nvim-treesitter",
    version = "0.21.0",
    build = ":TSUpdate",
    dependencies = {
        "nvim-treesitter/playground",  -- 可选，查看语法树
        "nvim-treesitter/nvim-treesitter-textobjects", -- 可选
    },
    opts = {
        ensure_installed = {"c", "cpp", "cangjie"},
        sync_install = false,
        highlight = { enable = true, additional_vim_regex_highlighting = false },
        indent = { enable = true },
        incremental_selection = { enable = true }, -- 增量选择
        -- 可选
        playground = {
            enable = true, -- 启用 playground
            updatetime = 25, -- 刷新延迟（以毫秒为单位）proxy
            persist_queries = false, -- 不保留查询（重启后清空）
            keybindings = {
                toggle_query_editor = "o", -- 打开/关闭查询编辑器
                toggle_hl_groups = "i", -- 打开/关闭高亮组
                toggle_injected_languages = "t", -- 打开/关闭嵌套语言
                toggle_anonymous_nodes = "a", -- 打开/关闭匿名节点
                toggle_language_display = "I", -- 显示/隐藏语言名称
                focus_language = "f", -- 聚焦到语言
                unfocus_language = "F", -- 取消聚焦
                update = "R", -- 手动刷新
                goto_node = "<cr>", -- 跳转到语法树节点
                show_help = "?", -- 显示帮助
            },
        },
    },
}

M.config = function(_, opts)
    require("nvim-treesitter.configs").setup(opts) -- 配置 treesitter
    ---@class PasserConfig
    local parser_config = require("nvim-treesitter.parsers").get_parser_configs()
    parser_config.cangjie = {
        install_info = {
            url = "https://gitcode.com/Cangjie-SIG/tree-sitter-cangjie",
            files = { "src/parser.c", "src/scanner.c" },
            branch = "main",
            tag = "v1.0.0",
            generate_requires_npm = false,
            requires_generate_from_grammar = true,
        },
        filetype = "cangjie",
    }
end

return M
